//
//  BaseAlertView.m
//  GK
//
//  Created by Seven on 2019/4/16.
//  Copyright © 2019 Seven. All rights reserved.
//

#import "BaseAlertView.h"

@implementation BaseAlertView

+ (instancetype)showWithInfo:(id)info customAction:(CustomAction)action{
    UIView *sView = [UIApplication sharedApplication].keyWindow;
    BaseAlertView *view = [self initWithFrame:sView.bounds];
    view.customAction = action;
    [sView addSubview:view];
    view.info = info;
    [view updateWithInfo:info];
    [view show];
    return view;
}

- (void)initViews{
    [super initViews];
    self.alpha = 0;
    self.backgroundColor = COLOR_CUSTOM_ALPHA;
    
    CGFloat scale = kScreenWidth/375;
    self.scale = scale;
    _contentView = [BaseView initWithFrame:CGRectMake(0, 0, 320*scale, 205.5*scale)];
    _contentView.backgroundColor = COLOR_WHITE;
    [_contentView setCornerRadius:1];
    [self addSubview:_contentView];
    _contentView.center = self.center;
    
    BaseLabel *lbl = [BaseLabel initWithFrame:CGRectMake(50, 60, _contentView.width-100, 25)];
    lbl.font = NumberFontWithSize(30);
    lbl.textColor = COLOR_CUSTOM_YELLOW;
    lbl.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:lbl];
    _titleLbl = lbl;
    
    lbl = [BaseLabel initWithFrame:CGRectMake(50, 60, _contentView.width-100, 25)];
    lbl.font = defaultFontWithSize(12);
    lbl.textColor = COLOR_666666;
    lbl.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:lbl];
    _tipLbl = lbl;
    
    CGFloat closeWidth = 50;
    UIButton *btn = [UIButton initWithFrame:CGRectMake(_contentView.width-closeWidth, 0, closeWidth, closeWidth)];
    btn.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    btn.contentEdgeInsets = UIEdgeInsetsMake(10, 0, 0, 10);
    [btn setImage:[ImageNamed(@"ic_close") imageWithRenderingMode:(UIImageRenderingModeAlwaysTemplate)]];
    [btn setCornerRadius:btn.height/2];
    [btn addTarget:self action:@selector(onCancelClick)];
    btn.tintColor = COLOR_999999;
    [_contentView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.equalTo(_contentView);
        make.size.mas_equalTo(CGSizeMake(closeWidth, closeWidth));
    }];
    self.closeBtn = btn;
    
    btn = [UIButton initWithFrame:CGRectMake((_contentView.width-240)/2, 260, 240, 45)];
    [btn setBackgroundImage:ImageNamed(@"bg_button") forState:(UIControlStateNormal)];
    [btn setCornerRadius:btn.height/2];
    btn.titleLabel.font = defaultFontWithSize(18);
    [btn setTitleColor:ColorWithHex(0x6E3D00)];
    [btn addTarget:self action:@selector(onDoneClick)];
    [_contentView addSubview:btn];
    _doneBtn = btn;
    
}

- (void)updateWithInfo:(id)info{
    
}


- (void)setTitleFont:(UIFont *)font{
    _titleLbl.font = font;
}
- (void)setTipFont:(UIFont *)font{
    _tipLbl.font = font;
}

- (void)setTitleColor:(UIColor *)color{
    _titleLbl.textColor = color;
}
- (void)setTipColor:(UIColor *)color{
    _tipLbl.textColor = color;
}

- (void)setDontBtnName:(NSString *)name{
    [_doneBtn setTitleNormal:name];
}


- (void)onDoneClick{
    if (self.customAction) {
        self.customAction(self, cat_done);
    }
    [self hide];
}
- (void)onCancelClick{
    if (self.customAction) {
        self.customAction(self, cat_cancel);
    }
    [self hide];
}

- (void)show{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}
- (void)hide{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
